import pytest
from loguru import logger
from common.yaml_util import read_yaml
from pageobject.公共页面 import HomePage
from pageobject.业务变更页面 import BusinessChangePage
from testcases.test_订购_撤单 import TestOrderAndRevoke


@pytest.fixture(scope="session", autouse=True)
def data_init():
    if read_yaml('系统配置.yaml')['执行数据初始化']:
        logger.info('\n'+"*" * 70 + '数据初始化' + "*" * 70)
        for i in range(3):
            try:
                data = [dct for dct in read_yaml('数据初始化.yaml') if dct['地市'] == '沈阳[SY]'][0]
                hp = HomePage(data)
                hp.login()
                hp.enter_acceptance_bus()
                bcp = BusinessChangePage(data['用例信息'])
                unsubscribe_ls = bcp.get_init_list()
                for unsubscribe in unsubscribe_ls:
                    data = [dct for dct in read_yaml(unsubscribe['数据文件']) if dct['地市'] == '沈阳[SY]' and dct['撤单']['业务名称'] == unsubscribe['业务名称']][0]
                    eval(unsubscribe['方法名称'])(data)

                data = [dct for dct in read_yaml('数据初始化.yaml') if dct['地市'] == '阜新[FX]'][0]
                hp = HomePage(data)
                hp.login()
                hp.enter_acceptance_bus()
                bcp = BusinessChangePage(data['用例信息'])
                unsubscribe_ls = bcp.get_init_list()
                for unsubscribe in unsubscribe_ls:
                    data = [dct for dct in read_yaml(unsubscribe['数据文件']) if dct['地市'] == '阜新[FX]' and dct['撤单']['业务名称'] == unsubscribe['业务名称']][0]
                    eval(unsubscribe['方法名称'])(data)
                break
            except Exception:
                logger.warning("初始化发生异常")
        logger.info('\n'+"*" * 68 + '数据初始化完成' + "*" * 68)

